package org.example.Iterator;

import java.util.*;

public class SpliteratorTest {
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }

    private static void test1() {
        List<String> lists = Arrays.asList("A", "B", "C", "D");
        Spliterator<String> spliterator = lists.spliterator();
        while (spliterator.tryAdvance(System.out::println)) ;
    }

    private static void test2() {
        List<String> lists = Arrays.asList("A", "B", "C", "D");
        lists.spliterator().forEachRemaining(System.out::println);
    }

    private static void test3() {
        List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Strawberry", "Blueberry", "Peach", "Tomato", "Watermelon");
        Spliterator<String> s = list.spliterator();
        s.tryAdvance(System.out::println);  // Apple
        System.out.println();
        Spliterator<String> s1 = s.trySplit();
        s.forEachRemaining(System.out::println);    // Blueberry Peach Tomato Watermelon
        System.out.println();
        s1.forEachRemaining(System.out::println);   // Banana Orange Strawberry
    }

    private static void test4() {
        List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Strawberry", "Blueberry", "Peach", "Tomato", "Watermelon");
        Spliterator<String> s = list.spliterator();
        System.out.println(s.characteristics());                            // 16464
        // characteristics为对应特性的或运算
        System.out.println((Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED) == s.characteristics()); // true
        System.out.println(s.hasCharacteristics(Spliterator.SIZED));        // true
        System.out.println(s.hasCharacteristics(Spliterator.SUBSIZED));     // true
        System.out.println(s.hasCharacteristics(Spliterator.NONNULL));      // false
        System.out.println(s.hasCharacteristics(Spliterator.CONCURRENT));   // false
        System.out.println(s.hasCharacteristics(Spliterator.SORTED));       // false
        System.out.println(s.hasCharacteristics(Spliterator.DISTINCT));     // false
        System.out.println(s.hasCharacteristics(Spliterator.ORDERED));      // true
        System.out.println(s.hasCharacteristics(Spliterator.IMMUTABLE));    // false
    }

    private static void test5() {
        List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Strawberry", "Blueberry", "Peach", "Tomato", "Watermelon");
        SortedSet<String> set1 = new TreeSet<>(list);
        Spliterator<String> s = set1.spliterator();
        System.out.println(getCharacteristics(s));  // SIZED SORTED DISTINCT ORDERED
        System.out.println(s.getComparator());      // null

        SortedSet<String> set2 = new TreeSet<>((a, b) -> a.compareTo(b) > 0 ? 1 : 0);
        set2.addAll(list);
        s = set2.spliterator();
        System.out.println(getCharacteristics(s));  // SIZED SORTED DISTINCT ORDERED
        System.out.println(s.getComparator());      // org.example.IteratorTest.SpliteratorTest.Demo1$$Lambda$15/0x0000000800c01200@682a0b20
    }

    private static String getCharacteristics(Spliterator<String> s) {
        String re = "";
        re += s.hasCharacteristics(Spliterator.SIZED) ? "SIZED " : "";
        re += s.hasCharacteristics(Spliterator.SUBSIZED) ? "SUBSIZED " : "";
        re += s.hasCharacteristics(Spliterator.NONNULL) ? "NONNULL " : "";
        re += s.hasCharacteristics(Spliterator.CONCURRENT) ? "CONCURRENT " : "";
        re += s.hasCharacteristics(Spliterator.SORTED) ? "SORTED " : "";
        re += s.hasCharacteristics(Spliterator.DISTINCT) ? "DISTINCT " : "";
        re += s.hasCharacteristics(Spliterator.ORDERED) ? "ORDERED " : "";
        re += s.hasCharacteristics(Spliterator.IMMUTABLE) ? "IMMUTABLE " : "";
        return re;
    }

    private static void test6() {
        List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Strawberry", "Blueberry", "Peach", "Tomato", "Watermelon");
        Spliterator<String> s = list.spliterator();
        s.tryAdvance(System.out::println);
        Iterator<String> iterator = Spliterators.iterator(s);
        iterator.forEachRemaining(x -> System.out.print(x + "\t"));
        System.out.println();
        Iterator<String> iterator1 = list.iterator();
        System.out.println(iterator1.next());
        Spliterator<String> spliterator = Spliterators.spliterator(iterator1, list.size() - 1, Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED);
        spliterator.forEachRemaining(x -> System.out.print(x + "\t"));
    }
}
